package com.ruoyi.web.controller.warning;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.warning.domain.dto.EncodeDTO;
import com.ruoyi.warning.domain.entity.Encode;
import com.ruoyi.warning.domain.query.EncodeQuery;
import com.ruoyi.warning.service.ICameraService;
import com.ruoyi.warning.service.IEncodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 硬盘录像机表 前端控制器
 * </p>
 *
 * @author chenhc
 * @since 2025-09-30
 */
@RestController
@RequestMapping("/encode")
@Api(tags = "硬盘录像机", description = "用于处理硬盘录像机相关操作的控制器")
public class EncodeController {
    @Resource
    private IEncodeService iEncodeService;
    @Resource
    private ICameraService cameraService;

    @ApiOperation("硬盘录像机分页")
    //@PreAuthorize("@ss.hasPermi('system:config:export')")
    @GetMapping(value = "/page")
    public R<TableDataInfo> page(EncodeQuery query)
    {
        Page<Encode> page = PageHelper.startPage(query.getPageNum(),query.getPageSize());
        List<Encode> list = iEncodeService.list(new LambdaQueryWrapper<Encode>()
                .eq(query.getSettingId()!=null, Encode::getSettingId,query.getSettingId()));
        return R.ok(new TableDataInfo(list, page.getTotal()));
    }

    @ApiOperation("硬盘录像机列表")
    //@PreAuthorize("@ss.hasPermi('system:config:export')")
    @GetMapping(value = "/list")
    public R<Object> list(EncodeQuery query)
    {
        List<Encode> list = iEncodeService.list(new LambdaQueryWrapper<Encode>()
                .eq(query.getSettingId()!=null, Encode::getSettingId,query.getSettingId()));
        return R.ok(list);
    }

    @ApiOperation("硬盘录像机和监控的树")
    @GetMapping(value = "/tree")
    public R<Object> tree(EncodeQuery query)
    {
        List<EncodeDTO> list = iEncodeService.tree(query);
        return R.ok(list);
    }

    @ApiOperation("硬盘录像机刷新")
    @Anonymous
    //@PreAuthorize("@ss.hasPermi('system:config:export')")
    @GetMapping(value = "/refresh")
    public R<Object> refresh(EncodeQuery query)
    {
        List<Encode> list = cameraService.refreshEncode(query);
        iEncodeService.saveOrUpdateBatch(list);
        return R.ok();
    }

    @ApiOperation("硬盘录像机编辑")
    //@PreAuthorize("@ss.hasPermi('system:config:export')")
    @PostMapping(value = "/saveOrUpdate")
    public R<Object> saveOrUpdate(@RequestBody Encode entity)
    {
        iEncodeService.saveOrUpdate(entity);
        return R.ok();
    }

    @ApiOperation("硬盘录像机删除")
    @PostMapping(value = "/delete")
    public R<Object> delete(@RequestBody List<String> ids){
        iEncodeService.removeByIds(ids);
        return R.ok();
    }
}
